﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QuanLyTiemVang.Classes
{
    enum TypePhanQuyen
    {
        Admin,
        Staff,
        Customer
    }
    class CurrentUser
    {
        private TAIKHOAN taiKhoan;
        private List<IPhanQuyenHelper> listPhanQuyen;
        private CurrentUser() 
        {
            listPhanQuyen = new List<IPhanQuyenHelper>();
        }

        public void AddForm(IPhanQuyenHelper ipq)
        {
            listPhanQuyen.Add(ipq);
            switch (PhanQuyen)
            {
                case TypePhanQuyen.Admin:
                     ipq.SetAdminFunction();
                    break;
                case TypePhanQuyen.Staff:
                    ipq.SetStaffFunction();
                    break;
                case TypePhanQuyen.Customer:
                    ipq.SetCustomerFunction();
                    break;
            }
        }

        public TAIKHOAN TaiKhoan
        {
            get { return taiKhoan; }
        }        

        public TypePhanQuyen PhanQuyen
        {
            get
            {
                switch (taiKhoan.MaLoaiTaiKhoan)
                {
                    case 1:
                        return TypePhanQuyen.Admin;
                    case 2:
                        return TypePhanQuyen.Staff;
                    case 3:
                        return TypePhanQuyen.Customer;
                }
                throw new Exception("MaLoaiTaiKhoan " + taiKhoan.MaLoaiTaiKhoan + " doesn't exist in db");
            }
        }

        // server for purpose of LogWriter class
        public override string ToString()
        {
            if (taiKhoan != null)
                return "Account: " + taiKhoan.TenDangNhap;
            return "Not User Available";
        }

        public delegate void SignInOutDelegate();
        public event SignInOutDelegate OnSignIn, OnSignOut;        

        public void SignIn(TAIKHOAN tk)
        {
            // remove nulll items
            for (int i = 0; i < listPhanQuyen.Count; i++)
            {
                if (listPhanQuyen[i] == null)
                {
                    listPhanQuyen.RemoveAt(i);
                    --i;
                }
            }
            taiKhoan = tk;

            switch (PhanQuyen)
            {
                case TypePhanQuyen.Admin:
                    foreach (IPhanQuyenHelper item in listPhanQuyen)
                        item.SetAdminFunction();
                    break;
                case TypePhanQuyen.Staff:
                    foreach (IPhanQuyenHelper item in listPhanQuyen)
                        item.SetStaffFunction();
                    break;
                case TypePhanQuyen.Customer:
                    foreach (IPhanQuyenHelper item in listPhanQuyen)
                        item.SetCustomerFunction();
                    break;
            }
            if (OnSignIn != null)              
                OnSignIn();
        }

        public void SignOut()
        {
            // remove nulll items
            for (int i = 0; i < listPhanQuyen.Count; i++)
            {
                if (listPhanQuyen[i] == null)
                {
                    listPhanQuyen.RemoveAt(i);
                    --i;
                }
            }
            taiKhoan = null;            
            foreach (IPhanQuyenHelper item in listPhanQuyen)
                item.SetNonFunction();
            if (OnSignOut != null)
                OnSignOut();  
        }

        public static CurrentUser Instance
        {
            get 
            {                
                return instance;
            }
        }

        private static CurrentUser instance;

        // static constructor
        static CurrentUser()
        {
            instance = new CurrentUser();
        }
    }
}
